#include "CCU8.h"
#include "SVPWM.h"


void CCU80_Init(void)
{
	WR_REG(SCU_RESET->PRSET0, SCU_RESET_PRSET0_CCU80RS_Msk, SCU_RESET_PRSET0_CCU80RS_Pos, 1);
	WR_REG(SCU_RESET->PRCLR0, SCU_RESET_PRCLR0_CCU80RS_Msk, SCU_RESET_PRCLR0_CCU80RS_Pos, 1);	
	WR_REG(SCU_CLK->CLKSET, SCU_CLK_CLKSET_CCUCEN_Msk, SCU_CLK_CLKSET_CCUCEN_Pos, 1);		

	WR_REG(CCU80->GIDLC, CCU8_GIDLC_SPRB_Msk, CCU8_GIDLC_SPRB_Pos, 1);
	
	
	//CCU80_CC80
	WR_REG(CCU80_CC80->INS, CCU8_CC8_INS_EV0IS_Msk, CCU8_CC8_INS_EV0IS_Pos, 7);	
	WR_REG(CCU80_CC80->INS, CCU8_CC8_INS_EV0EM_Msk, CCU8_CC8_INS_EV0EM_Pos, 1);		
	WR_REG(CCU80_CC80->CMC, CCU8_CC8_CMC_STRTS_Msk, CCU8_CC8_CMC_STRTS_Pos, 1);	
	WR_REG(CCU80_CC80->TC, CCU8_CC8_TC_MCME1_Msk, CCU8_CC8_TC_MCME1_Pos, 1);			
	WR_REG(CCU80_CC80->TC, CCU8_CC8_TC_TCM_Msk, CCU8_CC8_TC_TCM_Pos, 1);		
	WR_REG(CCU80_CC80->TC, CCU8_CC8_TC_STRM_Msk, CCU8_CC8_TC_STRM_Pos, 1);		
	
	WR_REG(CCU80_CC80->DC1R, CCU8_CC8_DC1R_DT1F_Msk, CCU8_CC8_DC1R_DT1F_Pos, 60);
	WR_REG(CCU80_CC80->DC1R, CCU8_CC8_DC1R_DT1R_Msk, CCU8_CC8_DC1R_DT1R_Pos, 60);
	WR_REG(CCU80_CC80->DTC, CCU8_CC8_DTC_DTE1_Msk, CCU8_CC8_DTC_DTE1_Pos, 1);
	WR_REG(CCU80_CC80->DTC, CCU8_CC8_DTC_DCEN1_Msk, CCU8_CC8_DTC_DCEN1_Pos, 1);
	WR_REG(CCU80_CC80->DTC, CCU8_CC8_DTC_DCEN2_Msk, CCU8_CC8_DTC_DCEN2_Pos, 1);
	
	WR_REG(CCU80_CC80->PRS, CCU8_CC8_PRS_PRS_Msk, CCU8_CC8_PRS_PRS_Pos, SVPWM16.PWM_Period);
	WR_REG(CCU80_CC80->CR1S, CCU8_CC8_CR1S_CR1S_Msk, CCU8_CC8_CR1S_CR1S_Pos, SVPWM16.PWM_Period+1);
	
	
	//CCU80_CC81
	WR_REG(CCU80_CC81->INS, CCU8_CC8_INS_EV0IS_Msk, CCU8_CC8_INS_EV0IS_Pos, 7);	
	WR_REG(CCU80_CC81->INS, CCU8_CC8_INS_EV0EM_Msk, CCU8_CC8_INS_EV0EM_Pos, 1);		
	WR_REG(CCU80_CC81->CMC, CCU8_CC8_CMC_STRTS_Msk, CCU8_CC8_CMC_STRTS_Pos, 1);	
	WR_REG(CCU80_CC81->TC, CCU8_CC8_TC_MCME1_Msk, CCU8_CC8_TC_MCME1_Pos, 1);	
	WR_REG(CCU80_CC81->TC, CCU8_CC8_TC_TCM_Msk, CCU8_CC8_TC_TCM_Pos, 1);		
	WR_REG(CCU80_CC81->TC, CCU8_CC8_TC_STRM_Msk, CCU8_CC8_TC_STRM_Pos, 1);	
	
	WR_REG(CCU80_CC81->DC1R, CCU8_CC8_DC1R_DT1F_Msk, CCU8_CC8_DC1R_DT1F_Pos, 60);
	WR_REG(CCU80_CC81->DC1R, CCU8_CC8_DC1R_DT1R_Msk, CCU8_CC8_DC1R_DT1R_Pos, 60);
	WR_REG(CCU80_CC81->DTC, CCU8_CC8_DTC_DTE1_Msk, CCU8_CC8_DTC_DTE1_Pos, 1);
	WR_REG(CCU80_CC81->DTC, CCU8_CC8_DTC_DCEN1_Msk, CCU8_CC8_DTC_DCEN1_Pos, 1);
	WR_REG(CCU80_CC81->DTC, CCU8_CC8_DTC_DCEN2_Msk, CCU8_CC8_DTC_DCEN2_Pos, 1);
	
	WR_REG(CCU80_CC81->PRS, CCU8_CC8_PRS_PRS_Msk, CCU8_CC8_PRS_PRS_Pos, SVPWM16.PWM_Period);
	WR_REG(CCU80_CC81->CR1S, CCU8_CC8_CR1S_CR1S_Msk, CCU8_CC8_CR1S_CR1S_Pos, SVPWM16.PWM_Period+1);
	
	
	//CCU80_CC82
	WR_REG(CCU80_CC82->INS, CCU8_CC8_INS_EV0IS_Msk, CCU8_CC8_INS_EV0IS_Pos, 7);	
	WR_REG(CCU80_CC82->INS, CCU8_CC8_INS_EV0EM_Msk, CCU8_CC8_INS_EV0EM_Pos, 1);		
	WR_REG(CCU80_CC82->CMC, CCU8_CC8_CMC_STRTS_Msk, CCU8_CC8_CMC_STRTS_Pos, 1);	
	WR_REG(CCU80_CC82->TC, CCU8_CC8_TC_MCME1_Msk, CCU8_CC8_TC_MCME1_Pos, 1);	
	WR_REG(CCU80_CC82->TC, CCU8_CC8_TC_TCM_Msk, CCU8_CC8_TC_TCM_Pos, 1);
	WR_REG(CCU80_CC82->TC, CCU8_CC8_TC_STRM_Msk, CCU8_CC8_TC_STRM_Pos, 1);	
		
	WR_REG(CCU80_CC82->DC1R, CCU8_CC8_DC1R_DT1F_Msk, CCU8_CC8_DC1R_DT1F_Pos, 60);
	WR_REG(CCU80_CC82->DC1R, CCU8_CC8_DC1R_DT1R_Msk, CCU8_CC8_DC1R_DT1R_Pos, 60);
	WR_REG(CCU80_CC82->DTC, CCU8_CC8_DTC_DTE1_Msk, CCU8_CC8_DTC_DTE1_Pos, 1);
	WR_REG(CCU80_CC82->DTC, CCU8_CC8_DTC_DCEN1_Msk, CCU8_CC8_DTC_DCEN1_Pos, 1);
	WR_REG(CCU80_CC82->DTC, CCU8_CC8_DTC_DCEN2_Msk, CCU8_CC8_DTC_DCEN2_Pos, 1);
	
	WR_REG(CCU80_CC82->PRS, CCU8_CC8_PRS_PRS_Msk, CCU8_CC8_PRS_PRS_Pos, SVPWM16.PWM_Period);
	WR_REG(CCU80_CC82->CR1S, CCU8_CC8_CR1S_CR1S_Msk, CCU8_CC8_CR1S_CR1S_Pos, SVPWM16.PWM_Period+1);

	WR_REG(CCU80_CC81->INTE, CCU8_CC8_INTE_PME_Msk, CCU8_CC8_INTE_PME_Pos, 1);
	WR_REG(CCU80_CC81->SRS, CCU8_CC8_SRS_POSR_Msk, CCU8_CC8_SRS_POSR_Pos, 2);

	//interrupt
	WR_REG(CCU80_CC80->INTE, CCU8_CC8_INTE_OME_Msk, CCU8_CC8_INTE_OME_Pos, 1);
	WR_REG(CCU80_CC80->SRS, CCU8_CC8_SRS_POSR_Msk, CCU8_CC8_SRS_POSR_Pos, 0);
	NVIC_SetPriority(CCU80_0_IRQn, 0);
	NVIC_EnableIRQ(CCU80_0_IRQn);
	 
	
	//transfer
	WR_REG(CCU80->GCSS, CCU8_GCSS_S0SE_Msk, CCU8_GCSS_S0SE_Pos, 1);
	WR_REG(CCU80->GCSS, CCU8_GCSS_S1SE_Msk, CCU8_GCSS_S1SE_Pos, 1);	
	WR_REG(CCU80->GCSS, CCU8_GCSS_S2SE_Msk, CCU8_GCSS_S2SE_Pos, 1);	
	WR_REG(CCU80->GIDLC, CCU8_GIDLC_CS0I_Msk, CCU8_GIDLC_CS0I_Pos, 1);
	WR_REG(CCU80->GIDLC, CCU8_GIDLC_CS1I_Msk, CCU8_GIDLC_CS1I_Pos, 1);
	WR_REG(CCU80->GIDLC, CCU8_GIDLC_CS2I_Msk, CCU8_GIDLC_CS2I_Pos, 1);
	

}


void CCU80_Start(void)
{
	WR_REG(SCU_GENERAL->CCUCON, SCU_GENERAL_CCUCON_GSC80_Msk, SCU_GENERAL_CCUCON_GSC80_Pos, 1);
}
